package com.financing.platform.web;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.financing.platform.dto.ApiSettingDTO;
import com.financing.platform.service.IApiSettingService;
import com.financing.platform.util.DTOdateSet;
import com.financing.platform.util.Dict;

@Controller
public class ApiSettingAction extends BaseAction{
	
	@Autowired
	private IApiSettingService apiSettingDaoImpl;
	
	@RequestMapping("/queryApiSetting.shtml")
	public ModelAndView queryApiSetting(HttpServletRequest request,
			HttpServletResponse response) {
			request.getSession().setAttribute(Dict.requestID, "queryApiSetting.shtml");
			List<ApiSettingDTO> apiSettingList = null;
			
			try {
				apiSettingList = apiSettingDaoImpl.queryApiSetting();
			} catch (SQLException e) {
				log.error("query api setting error",e);
			}
			Map<String,Object> resultMap = new HashMap<String,Object>();
			resultMap.put("apiSettingList", apiSettingList);
			return new ModelAndView("apiSettingManager",resultMap);
	}
	
	@RequestMapping("/updateApiSetting.shtml")
	@ResponseBody
	public Map<?,?> updateApiSetting(HttpServletRequest request,
			HttpServletResponse response,ApiSettingDTO apiSettingDTO) {
		
			DTOdateSet.setUpdateInfo(apiSettingDTO, request.getSession());
			
			Map<String,Object> resultMap = new HashMap<String,Object>();
			try {
				apiSettingDaoImpl.updateApiSetting(apiSettingDTO);
				resultMap.put(Dict.result, Dict.result_success);
			} catch (SQLException e) {
				log.error("update Api setting error!",e);
				resultMap.put(Dict.result, Dict.result_error);
			}
			
			return resultMap;
	}
	
	@RequestMapping("/addApiSetting.shtml")
	@ResponseBody
	public Map<?,?> addApiSetting(HttpServletRequest request,
			HttpServletResponse response,ApiSettingDTO apiSettingDTO) {
		
			DTOdateSet.setUpdateInfo(apiSettingDTO, request.getSession());
			DTOdateSet.setCreateInfo(apiSettingDTO, request.getSession());
			
			Map<String,Object> resultMap = new HashMap<String,Object>();
			try {
				apiSettingDaoImpl.addApiSetting(apiSettingDTO);
				resultMap.put(Dict.result, Dict.result_success);
			} catch (SQLException e) {
				log.error("update Api setting error!",e);
				resultMap.put(Dict.result, Dict.result_error);
			}
			
			return resultMap;
	}
}
